package com.sit.jike.hrproject.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sit.jike.hrproject.bean.SearchBean;
import com.sit.jike.hrproject.entity.HrDept;
import com.sit.jike.hrproject.mapper.HrDeptMapper;
import com.sit.jike.hrproject.service.IHrDeptService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Allen
 * @since 2025-06-18
 */
@Service
public class HrDeptServiceImpl extends ServiceImpl<HrDeptMapper, HrDept> implements IHrDeptService {
    @Autowired
    private HrDeptMapper hrDeptMapper;

    @Override
    public String hello() {
        return "hello AAAA";
    }

    @Override
    public List<HrDept> getAll(String deptName) {
        QueryWrapper<HrDept> queryWrapper = new QueryWrapper<>();
        if(deptName!=null&&!"".equals(deptName)){
            queryWrapper.like("dept_name",deptName);
        }

        return hrDeptMapper.selectList(queryWrapper);
    }

    @Override
    public List<Map<String, Object>> getAllMap(String deptName) {
        if(StringUtils.isNotBlank(deptName)){
            deptName = "%"+deptName+"%";
        }

        return hrDeptMapper.getAllMap(deptName);
    }

    @Override
    public IPage<Map<String, Object>> getAllMapWithPage(SearchBean bean) {
        IPage<Map<String, Object>> page = new Page<>(bean.getPage(), bean.getLimit());
        return hrDeptMapper.getAllMapWithPage(page, bean.getDeptName4Like());
    }

    @Override
    public String deleteDept(String id) {
        hrDeptMapper.deleteById(id);
        return "ok";
    }

    @Override
    public String saveDeptInfo(HrDept hrDept) {
        if (hrDept.getId()==0) {
            hrDept.setCreateTime(LocalDateTime.now());
            hrDeptMapper.insert(hrDept);

        } else {
            hrDeptMapper.updateById(hrDept);
        }

        return "ok";
    }
}
